//
//  ViewController.m
//  globalTimerDemo
//
//  Created by wanglei on 17/3/10.
//  Copyright © 2017年 wanglei. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property(nonatomic,strong)NSTimer *timer; // timer
@property(nonatomic,assign)int countDown; // 倒数计时用
@property(nonatomic,strong)NSDate *beforeDate; // 上次进入后台时间
@end

static int const tick = 60;
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNotification];
    [self startCountDown];

}
-(void)dealloc {
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIApplicationWillEnterForegroundNotification object:nil];
    [self stopTimer];
}

-(void)setupNotification {
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(enterBG) name:UIApplicationDidEnterBackgroundNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(enterFG) name:UIApplicationWillEnterForegroundNotification object:nil];
}

/**
 *  进入后台记录当前时间
 */
-(void)enterBG {
    NSLog(@"应用进入后台啦");
    _beforeDate = [NSDate date];
}

/**
 *  返回前台时更新倒计时值
 */
-(void)enterFG {
    NSLog(@"应用将要进入到前台");
    NSDate * now = [NSDate date];
    int interval = (int)ceil([now timeIntervalSinceDate:_beforeDate]);
    int val = _countDown - interval;
    if(val > 1){
        _countDown -= interval;
    }else{
        _countDown = 1;
    }
}

/**
 *  开始倒计时
 */
-(void)startCountDown {
    _countDown = tick; ///< 重置计时
    _timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES]; ///< 需要加入手动RunLoop，需要注意的是在NSTimer工作期间self是被强引用的
    [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes]; ///< 使用NSRunLoopCommonModes才能保证RunLoop切换模式时，NSTimer能正常工作。
}

-(void)timerFired:(NSTimer *)timer {
    if (_countDown == 0) {
        [self stopTimer];
        NSLog(@"重新发送");
    }else{
        _countDown -=1;
        NSLog(@"倒计时中：%d",_countDown);
    }
}

- (void)stopTimer {
    if (_timer) {
        [_timer invalidate];
    }
}

@end
